package org.csu.newpetstoreback.controller.front;

import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.entity.Comment;
import org.csu.newpetstoreback.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/comment/")
public class CommentController {

    @Autowired
    private CommentService commentService;

    //获取某item的所有评论
    @GetMapping("comments/itemid/{itemid}")
    @ResponseBody
    public CommonResponse<List<Comment>> getCommentsByItemid(@PathVariable("itemid")String itemId) {
        return commentService.getCommentsByItemid(itemId);
    }

    //获取某用户的所有评论
    @GetMapping("comments/username/{username}")
    @ResponseBody
    public CommonResponse<List<Comment>> getCommentsByUsername(@PathVariable("username")String username) {
        return commentService.getCommentsByUsername(username);
    }

    //新增一条评论
    @PostMapping("comments")
    @ResponseBody
    public CommonResponse<Comment> addComment(@RequestBody Comment comment) {

        //commentId我们自己生成，不要用户传
        int commentNum = commentService.getCommentNum();
        comment.setCommentId(commentNum + 1);

        return commentService.addComment(comment);
    }

    //删除指定评论
    @DeleteMapping("comments/{commentid}")
    @ResponseBody
    public CommonResponse<Comment> deleteCommentById(@PathVariable("commentid")int commentId) {
        return commentService.deleteCommentById(commentId);
    }
}
